package com.ictech.print.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.format.Formatter;
import android.util.Log;

import com.ictech.print.BaseApplication;

public class NetworkUtil {
	
	public static boolean isWifiConnected() {
		ConnectivityManager connMgr = (ConnectivityManager) BaseApplication.app.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
	    return (networkInfo != null && networkInfo.isConnected());
	}
	
	public static void closeWifi() {
		WifiManager wifiManager = (WifiManager) BaseApplication.app.getSystemService(Context.WIFI_SERVICE);
		if (wifiManager.isWifiEnabled()) {
			wifiManager.setWifiEnabled(false);
		}
	}

	public static boolean isWifiEnabled() {
		WifiManager wifiManager = (WifiManager) BaseApplication.app.getSystemService(Context.WIFI_SERVICE);
		return wifiManager.isWifiEnabled();	
	}
	
	public static String getGatewayIp() {  
		try {
			WifiManager wifi_service = (WifiManager) BaseApplication.app.getSystemService(Context.WIFI_SERVICE);  
	        DhcpInfo dhcpInfo = wifi_service.getDhcpInfo();  
	        WifiInfo wifiinfo = wifi_service.getConnectionInfo();  
	        return Formatter.formatIpAddress(dhcpInfo.gateway);
		} catch (Exception e) {
		}
        return "192.168.1.1";
//        System.out.println("Wifi info----->"+wifiinfo.getIpAddress());  
//        System.out.println("DHCP info gateway----->"+Formatter.formatIpAddress(dhcpInfo.gateway));  
//        System.out.println("DHCP info netmask----->"+Formatter.formatIpAddress(dhcpInfo.netmask));  
//        //DhcpInfo中的ipAddress是一个int型的变量，通过Formatter将其转化为字符串IP地址  
//        return Formatter.formatIpAddress(dhcpInfo.ipAddress);  
    }
	
	public static String getWifiSsid() {
		try {
			WifiManager wifiManager = (WifiManager) BaseApplication.app.getSystemService(Context.WIFI_SERVICE);
			// 判断wifi是否开启
			if (wifiManager.isWifiEnabled()) {
				WifiInfo wifiInfo = wifiManager.getConnectionInfo();
				return wifiInfo.getSSID();
			}
		} catch (Exception e) {
		}
		return "未连接wifi";
	}
	
	/**
	 * 获取ip的
	 * @return
	 */
	public static String getWifiIp() {
		try {
			WifiManager wifiManager = (WifiManager) BaseApplication.app.getSystemService(Context.WIFI_SERVICE);
			// 判断wifi是否开启
			if (wifiManager.isWifiEnabled()) {
				WifiInfo wifiInfo = wifiManager.getConnectionInfo();
				int ipAddress = wifiInfo.getIpAddress();
				String ip = intToIp(ipAddress);
				return ip;
			} else {
				return "wifi 未连接";
			}
		} catch (Exception e) {
			return e.getMessage();
		}
	}

	private static String intToIp(int i) {
		return (i & 0xFF) + "." 
					+ ((i >> 8) & 0xFF) + "." 
					+ ((i >> 16) & 0xFF) + "." 
					+ (i >> 24 & 0xFF);
	}

	/**
	 * GPRS 上网的时候的ip
	 * 
	 * @return
	 */
	public static String getLocalIpAddress() {
		try {
			Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces();
			for (; en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
				for (; enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			Log.e("WifiPreference IpAddress", ex.toString());
		}
		return "";
	}

}
